package com.ykdl.member.kid.fittool;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import net.wxxr.dataparse.model.DataParseError;
import net.wxxr.dataparse.model.DataState;
import net.wxxr.http.interfaces.ITag;

import android.app.ProgressDialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.ykdl.member.kid.R;
import com.ykdl.member.dictionary.DictionaryDataManager;
import com.ykdl.member.http.Network;
import com.ykdl.member.kid.beans.BreastFeeding;
import com.ykdl.member.kid.beans.Nappy;
import com.ykdl.member.kid.beans.StatuResult;
import com.ykdl.member.kid.util.AssertTool;
import com.ykdl.member.kid.util.CalendarUtil;
import com.ykdl.member.kid.widget.RefreshListView;
import com.ykdl.member.views.ArrayWheelAdapter;
import com.ykdl.member.views.DateWheelAdapter;
import com.ykdl.member.views.NumericWheelAdapter;
import com.ykdl.member.views.OnWheelChangedListener;
import com.ykdl.member.views.OnWheelScrollListener;
import com.ykdl.member.views.WheelView;

public class AddNappy implements OnClickListener{
	
	private Context context;
	private ViewGroup group;
	private RefreshListView mListView;
	private TextView mTxtTitle;
	private TextView mTxtExcrete, mTxtSpill, mTxtTime;
	private Button mBtnClose, mBtnBack, mBtnNext, mBtnComplate;
	private View mLayerExcrete, mLayerSpill, mLayerTime;
	private WheelView mWheelView1, mWheelView2,mWheelView3,mWheelView4,mWheelView5,mWheelView6,mWheelView7;
	
	private List<String> listNappy, listShape, listColor;
	private String[] item1, item2, item3;
	private Nappy nappy = new Nappy();
	private DateWheelAdapter mDateAdapter;
	private CalendarUtil mCalendarUtil;
	private ProgressDialog mProgressDialog;
	
	private OnRefreshListListener mListListener;
	
	public AddNappy(Context context, ViewGroup group, RefreshListView listview){
		this.context = context;
		this.group = group;
		this.mListView = listview;
		this.mListListener = (OnRefreshListListener)context;
		View view = ((LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.layout_feed_tool_nappy, group);
		
		mCalendarUtil = new CalendarUtil();
		
		
		mTxtExcrete = (TextView) view.findViewById(R.id.txtExcrete);
		mTxtSpill = (TextView) view.findViewById(R.id.txtSpill);
		mTxtTime = (TextView) view.findViewById(R.id.txtTime);
		
		mTxtTitle = (TextView) view.findViewById(R.id.txtTitle);
		mTxtTitle.setText("代谢类型");
		
		mBtnClose = (Button) view.findViewById(R.id.btnClose);
		mBtnClose.setOnClickListener(this);
		mBtnBack = (Button) view.findViewById(R.id.btnBack);
		mBtnBack.setOnClickListener(this);
		mBtnNext = (Button) view.findViewById(R.id.btnNext);
		mBtnNext.setOnClickListener(this);
		mBtnComplate = (Button) view.findViewById(R.id.btnComplate);
		mBtnComplate.setOnClickListener(this);
		
		listNappy = DictionaryDataManager.getInstance().getParentDataList(context, AssertTool.NAPPY);
		
		listShape = DictionaryDataManager.getInstance().getParentDataList(context, AssertTool.DEFECATE_SHAPE);
		item2 = new String[listShape.size()];
		listShape.toArray(item2);
		listColor = DictionaryDataManager.getInstance().getParentDataList(context, AssertTool.DEFECATE_COLOR);
		item3 = new String[listColor.size()];
		listColor.toArray(item3);
		
		mLayerExcrete = view.findViewById(R.id.layerExcrete);
		mWheelView1 = (WheelView) view.findViewById(R.id.wheelView1);
		String[] item1 = new String[listNappy.size()];
		listNappy.toArray(item1);
		ArrayWheelAdapter<String> adapter1 = new ArrayWheelAdapter<String>(item1);
		mWheelView1.setAdapter(adapter1);
		mWheelView2 = (WheelView) view.findViewById(R.id.wheelView2);
		ArrayWheelAdapter<String> adapter2 = new ArrayWheelAdapter<String>(WeiYanData.empty);
		mWheelView2.setAdapter(adapter2);
		mWheelView3 = (WheelView) view.findViewById(R.id.wheelView3);
		ArrayWheelAdapter<String> adapter3 = new ArrayWheelAdapter<String>(WeiYanData.empty);
		mWheelView3.setAdapter(adapter3);
		
		mWheelView1.addChangingListener(new OnWheelChangedListener() {
			
			@Override
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				if (newValue != 0) {
					mWheelView2.setAdapter(new ArrayWheelAdapter<String>(item2));
					mWheelView3.setAdapter(new ArrayWheelAdapter<String>(item3));
				} else {
					mWheelView2.setAdapter(new ArrayWheelAdapter<String>(WeiYanData.empty));
					mWheelView3.setAdapter(new ArrayWheelAdapter<String>(WeiYanData.empty));
				}
			}
		});
		
		mLayerSpill = view.findViewById(R.id.layerSpill);
		mWheelView4 = (WheelView) view.findViewById(R.id.wheelView4);
		String[] spill = {"无溢出", "有溢出"};
		mWheelView4.setAdapter(new ArrayWheelAdapter<String>(spill));
		
		mLayerTime = view.findViewById(R.id.layerTime);
		
		mWheelView5 = (WheelView) view.findViewById(R.id.wheelView5);
		Calendar end = Calendar.getInstance();
		long time = end.getTimeInMillis();
		mDateAdapter = new DateWheelAdapter(time-(24*60*60*1000*14), time);
		mWheelView5.setAdapter(mDateAdapter);
		mWheelView6 = (WheelView) view.findViewById(R.id.wheelView6);
		mWheelView6.setAdapter(new NumericWheelAdapter(0, 23));
		mWheelView7 = (WheelView) view.findViewById(R.id.wheelView7);
		mWheelView7.setAdapter(new NumericWheelAdapter(0, 59));
		
	}
	

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btnClose:
			group.removeAllViews();
			group.setVisibility(View.GONE);
			mListView.setEnabled(true);
			break;
		case R.id.btnBack:
			if (mLayerTime.getVisibility() == View.VISIBLE) {
				mLayerTime.setVisibility(View.GONE);
				mLayerSpill.setVisibility(View.VISIBLE);
				mBtnComplate.setVisibility(View.GONE);
				mBtnNext.setVisibility(View.VISIBLE);
			} else if (mLayerSpill.getVisibility() == View.VISIBLE) {
				mLayerExcrete.setVisibility(View.VISIBLE);
				mLayerSpill.setVisibility(View.GONE);
				mBtnBack.setVisibility(View.GONE);
				mBtnClose.setVisibility(View.VISIBLE);
			}
			break;
		case R.id.btnNext:
			if (mLayerExcrete.getVisibility() == View.VISIBLE) {
				int curItem = mWheelView1.getCurrentItem();
				nappy.setExcrete_type(curItem+1);
				if (curItem == 0) {
					nappy.setShape(0);
					nappy.setColor(0);
				} else {
					nappy.setShape(mWheelView2.getCurrentItem()+1);
					nappy.setColor(mWheelView3.getCurrentItem()+1);
				}
				String text = mWheelView1.getCurrentItem() == 0 ? listNappy.get(0):
					listNappy.get(mWheelView1.getCurrentItem())+listShape.get(mWheelView2.getCurrentItem())+listColor.get(mWheelView3.getCurrentItem());
				mTxtExcrete.setText(text);
				mLayerExcrete.setVisibility(View.GONE);
				mBtnClose.setVisibility(View.GONE);
				mBtnBack.setVisibility(View.VISIBLE);
				mLayerSpill.setVisibility(View.VISIBLE);
				mTxtTitle.setText("有无溢出");
			} else if (mLayerSpill.getVisibility() == View.VISIBLE) {
				nappy.setSpill_over(mWheelView4.getCurrentItem());
				mTxtSpill.setText(nappy.getSpill_over()==0?"无溢出":"有溢出");
				mLayerSpill.setVisibility(View.GONE);
				mBtnClose.setVisibility(View.GONE);
				mBtnBack.setVisibility(View.VISIBLE);
				mBtnNext.setVisibility(View.INVISIBLE);
				mBtnComplate.setVisibility(View.VISIBLE);
				mLayerTime.setVisibility(View.VISIBLE);
				mTxtTitle.setText("完成时间");
				
			}
			break;
		case R.id.btnComplate:
			if (mLayerTime.getVisibility() == View.VISIBLE) {
				int item5 = mWheelView5.getCurrentItem();
				String month, day;
				String strDate = mDateAdapter.getItem(mWheelView5.getCurrentItem());
				if (strDate.equals("今天")) {
					String[] str = new SimpleDateFormat("MM-dd").format(new Date()).split("-");
					month = str[0];
					day = str[1];
				} else {
					String[] str = strDate.split("月");
					month = str[0];
					day = str[1].replace("日", "");
				}
				int hour = mWheelView6.getCurrentItem();
				int minute = mWheelView7.getCurrentItem();
				long time = mCalendarUtil.getLongByString(month, day, String.valueOf(hour), String.valueOf(minute));
				nappy.setCreate_time(time/1000);
				
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
				mTxtTime.setText(sdf.format(new Date(time)));
				submitData(nappy);
			}
			break;
		default:
			break;
		}
	}
	
	private void submitData(Nappy nappy) {
		mProgressDialog = ProgressDialog.show(context, null, "正在提交...");
		Network.getInstance().addFeedBean(nappy, new AddDataTag());
	}
	
	private class AddDataTag implements ITag<StatuResult>{

		@Override
		public void refreshUI(StatuResult t, DataState ds) {
			mProgressDialog.dismiss();
			if (t != null) {
				// 是否刷新视图 ？？
				group.removeAllViews();
				group.setVisibility(View.GONE);
				mListView.setEnabled(true);
				mListListener.refreshList();
			} else {
				Toast.makeText(context, "添加数据失败", 1).show();
			}
		}

		@Override
		public boolean refreshError(DataParseError parseError) {
			// TODO Auto-generated method stub
			mProgressDialog.dismiss();
			Toast.makeText(context, "添加数据失败", 1).show();
			return true;
		}

		@Override
		public void refreshCancel() {
			// TODO Auto-generated method stub
			
		}
		
	}
	
}
